package top.lyc829.zzl_android.ui.login;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.UserInfoBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class LoginViewModel extends ViewModel {

    public MutableLiveData<UserInfoBean> getUserInfoLiveData() {
        if (userInfoLiveData == null) {
            userInfoLiveData = new MutableLiveData<>();
        }
        return userInfoLiveData;
    }

    private MutableLiveData<UserInfoBean> userInfoLiveData;

    public void getUserInfo() {
        Network.getUserInfo(new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();

                ResultBean<UserInfoBean> resultBean = JsonUtilNew.fromJsonObject(resultStr, UserInfoBean.class);

                if (resultBean.getCode().equals(200)) {
                    getUserInfoLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout("获取用户信息:" + e.getMessage());
            }
        });
    }




}
